package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.GeTuiPushConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.GTPush;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.framework.web.service.GTPushService;
import com.ruoyi.system.domain.YxDeviceAndGetui;
import com.ruoyi.system.domain.YxDeviceLogUploadRecord;
import com.ruoyi.system.service.IYxDeviceAndGetuiService;
import com.ruoyi.system.service.IYxDeviceLogUploadRecordService;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Android日志上传Controller
 * 
 * @author lingyu
 * @date 2024-08-01
 */
@RestController
@RequestMapping("/system/deviceLogUploadRecord")
public class YxDeviceLogUploadRecordController extends BaseController
{

    @Autowired
    private IYxDeviceAndGetuiService yxDeviceAndGetuiService;

    @Autowired
    private IYxDeviceLogUploadRecordService yxDeviceLogUploadRecordService;

    @Autowired
    private GTPushService gtPushService;

    /**
     * 查询Android日志上传列表
     */
    @PreAuthorize("@ss.hasPermi('system:deviceLogUploadRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(YxDeviceLogUploadRecord yxDeviceLogUploadRecord)
    {
        startPage();
        List<YxDeviceLogUploadRecord> list = yxDeviceLogUploadRecordService.selectYxDeviceLogUploadRecordList(yxDeviceLogUploadRecord);
        return getDataTable(list);
    }

    /**
     * 导出Android日志上传列表
     */
    @PreAuthorize("@ss.hasPermi('system:deviceLogUploadRecord:export')")
    @Log(title = "Android日志上传", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YxDeviceLogUploadRecord yxDeviceLogUploadRecord)
    {
        List<YxDeviceLogUploadRecord> list = yxDeviceLogUploadRecordService.selectYxDeviceLogUploadRecordList(yxDeviceLogUploadRecord);
        ExcelUtil<YxDeviceLogUploadRecord> util = new ExcelUtil<YxDeviceLogUploadRecord>(YxDeviceLogUploadRecord.class);
        util.exportExcel(response, list, "Android日志上传数据");
    }

    /**
     * 获取Android日志上传详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:deviceLogUploadRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(yxDeviceLogUploadRecordService.selectYxDeviceLogUploadRecordById(id));
    }

    /**
     * 新增Android日志上传
     */
    @PreAuthorize("@ss.hasPermi('system:deviceLogUploadRecord:add')")
    @Log(title = "Android日志上传", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YxDeviceLogUploadRecord yxDeviceLogUploadRecord)
    {
        // 生成uuid
        yxDeviceLogUploadRecord.setUuid(IdUtils.fastSimpleUUID());
        // 并推送抓取日志消息
        if(!TextUtils.isEmpty(yxDeviceLogUploadRecord.getDeviceId())){
            //通知设备
            YxDeviceAndGetui yxDeviceAndGetui = new YxDeviceAndGetui();
            yxDeviceAndGetui.setDeviceId(yxDeviceLogUploadRecord.getDeviceId());
            List<YxDeviceAndGetui> yxDeviceAndGetuiList = yxDeviceAndGetuiService.selectYxDeviceAndGetuiList(yxDeviceAndGetui);
            if(yxDeviceAndGetuiList != null && yxDeviceAndGetuiList.size() == 1){

                if(gtPushService.isOnline(yxDeviceAndGetuiList.get(0).getCid())){
                    AsyncManager.me().execute(
                            AsyncFactory.transmissionPushByCid(
                                    yxDeviceAndGetuiList.get(0).getCid(),
                                    GTPush.create(
                                            GeTuiPushConstants.DEVICE_UPLOAD_LOG,
                                            yxDeviceLogUploadRecord.getUuid(),
                                            "上传本地日志")
                            )
                    );
                }else {
                    return error("cid异常 或者 设备不在线");
                }

            }else {
                return error("devicesId 不存在");
            }
        }else {
            return error("devicesId 不可为空");
        }
        return toAjax(yxDeviceLogUploadRecordService.insertYxDeviceLogUploadRecord(yxDeviceLogUploadRecord));
    }

    /**
     * 修改Android日志上传
     */
    @PreAuthorize("@ss.hasPermi('system:deviceLogUploadRecord:edit')")
    @Log(title = "Android日志上传", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YxDeviceLogUploadRecord yxDeviceLogUploadRecord)
    {
        return toAjax(yxDeviceLogUploadRecordService.updateYxDeviceLogUploadRecord(yxDeviceLogUploadRecord));
    }

    /**
     * 删除Android日志上传
     */
    @PreAuthorize("@ss.hasPermi('system:deviceLogUploadRecord:remove')")
    @Log(title = "Android日志上传", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yxDeviceLogUploadRecordService.deleteYxDeviceLogUploadRecordByIds(ids));
    }
}
